let items = {};
let add_js_tree_plugins = [];
let js_tree;
// $(function () {
//     init_all_js_tree();

//通过拖拽节点 为搜索框赋值 demo而已 可以根据业务需要 给别的输入框赋值
//     $("#search_ay").droppable({
//         drop: function (event, ui) {
//             if ($(this).attr("readonly") !== "readonly") {
//                 $(this).val($(this).val() + trim(ui.draggable.context.textContent));
//             }
//         }
//     });
// });

function init_all_js_tree() {
    try {
        $.each(js_tree_div_id, function (index, divId) {
            init_js_tree(divId);
            js_search(divId);
        });
    } catch (e) {
        console.error(e);
    }
}

function init_js_tree(divId) {
    js_tree = $('#' + divId + '').jstree({
        "core": {
            "animation": 0,
            "check_callback": true,
            "checkboxes": {
                "tie_selection": false,
                "whole_node": false
            },
            "multiple": true,
            "themes": {"stripes": true},
            'data': function (obj, callback) {
                try {
                    js_tree_data_list_async(function (result) {
                        callback.call(this, result);
                    });
                } catch (e) {
                    console.error(e);
                }
            },
            error: function (er) {
                console.log(er);
            }
        },
        "types": {
            "#": {
                "valid_children": ["root"]
            },
            "root": {
                "icon": "/static/3.3.5/assets/images/tree_icon.png",
                "valid_children": ["default"]
            },
            "default": {
                "valid_children": ["default", "file"],
                "icon": false
            },
            "file": {
                "icon": null,
                "valid_children": []
            }
        },
        "plugins": add_customer_plugins(),
        "search": {"show_only_matches": true},
        "contextmenu": {
            "items": add_customer_items()
        }
    }).bind("create_node.jstree", function (event, data) {
        if (isNull(update_node_async)) {
            console.error("缺少更新节点的函数");
            return false;
        }
        update_node_async(data.node);
    }).bind("rename_node.jstree", function (event, data) {
        if (isNull(rename_node_async)) {
            console.error("缺少重命名节点的函数");
            return false;
        }
        rename_node_async(data.node);
    }).bind("delete_node.jstree", function (event, data) {
        if (isNull(delete_node_async)) {
            console.error("缺少删除节点的函数");
            return false;
        }
        delete_node_async(data.node);
    }).bind("hover_node.jstree", function (event, data) {
        $("li").draggable({
            cursor: "move",
            cursorAt: {top: 10, left: -10},
            helper: function (event) {
                return $("<div style='position: fixed'>" + data.node.id + "</div>");
            },
            zIndex: 9999999,
            background: "white",
            distance: 20,
            containment: 'body'
        });
    }).bind("changed.jstree", function (event, data) {
        try {
            if (typeof selected_data_ids != 'undefined' && typeof eval(selected_data_ids) === "function") {
                selected_data_ids(data.selected);
            }
        } catch (e) {
            console.log(e);
        }
    }).bind("loaded.jstree", function (event, data) {
        data.instance.clear_state();
    });
}

function js_search(divId) {
    let to = false;
    $('#search_ay').keyup(function () {
        if (to) {
            clearTimeout(to);
        }
        to = setTimeout(function () {
            $('#' + divId + '').jstree(true).search($('#search_ay').val());
        }, 250);
    });
}


function get_parent_node_id_regexp(parentIdArray) {
    let result = '';
    for (let i = parentIdArray.length - 1; i >= 0; i--) {
        let element = parentIdArray[i];
        if (element === '#') {
            element = 0;
        }
        result = result.concat(element).concat(":");
    }
    return result;
}

function add_customer_plugins() {
    let default_customer_plugins = ["contextmenu", "search", "state", "types", "wholerow"];
    if (isNotNull(add_js_tree_plugins)) {
        $.each(add_js_tree_plugins, function (index, value) {
            default_customer_plugins.push(value);
        });
    }
    return default_customer_plugins;
}

function add_customer_items() {
    let default_items = {};
    if (isNotNull(items)) {
        $.each(items, function (key, value) {
            default_items[key] = value;
        });
    }
    return default_items;
}